#pragma once
#include<iostream>
namespace H
{
    template<class T>
    class vector {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        vector() 
        {
        }
        vector(size_t n, const T& val = T())
        {
            resize(n, val);
        }
        vector(int n, const T& val = T())
        {
            resize(n, val);
        }
        template<class InputIterator>
        vector(InputIterator begin, InputIterator end)
        {
            while (begin != end)
            {
                push_back(*begin);
                begin++;
            }
        }
        vector(const vector<T>& v)
        {
            _start = new T[v.capacity()];
            for (size_t i = 0; i < v.size(); i++)
            {
                _start[i] = v._start[i];
            }
            //_finish = v._finish; 只是令地址相等
            _finish = _start + v.size();
            _endofstorage = _start + v.capacity();
        }
        vector<T>& operator= (const vector<T>& v)
        {
            swap(v);
            return *this;
        }
        ~vector()
        {
            if (_start)
            {
                delete[]_start;
                _start = _finish = _endofstorage = nullptr;
            }
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin()const
        {
            return _start;
        }
        const_iterator end()const
        {
            return _finish;
        }
        size_t capacity()const
        {
            return _endofstorage - _start;
        }
        size_t size()const
        {
            return _finish - _start;
        }
        void resize(size_t n, const T& val)
        {
            if (n < size())
            {
                _finish = _start + n;
            }
            else
            {
                reserve(n);
                while (_finish != _start + n)
                {
                    *_finish = val;
                    _finish++;
                }
            }
        }
        void reserve(size_t n)
        {
            if (n > capacity())
            {
                size_t sz = size();
                T* tmp = new T[n];
                if (_start)
                {
                    memcpy(tmp, _start, sizeof(T) * size());
                    delete[] _start;
                }
                _start = tmp;
                _finish = _start + sz;
                _endofstorage = _start + n;
            }
        }
        void push_back(const T& x)
        {
            if (_finish == _endofstorage)
            {
                reserve(capacity() == 0 ? 1 : capacity() * 2);
            }
            *_finish = x;
            _finish++;
        }
        void swap(vector<T>v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endofstorage, v._endofstorage);
        }
        iterator insert(iterator pos, const T& val)
        {
            assert(pos < _finish&& pos >= _start);
            if (_finish == _endofstorage)
            {
                size_t newpos = pos - _start;
                reserve(capacity() == 0 ? 1 : capacity() * 2);
                pos = _start + newpos;
            }
            iterator end = _finish - 1;
            while (end >= pos)
            {
                *(end + 1) = end;
                end--;
            }
            *pos = val;
            ++_finish;
            return pos;

        }
        iterator erase(iterator pos)
        {
            assert(pos < _finish&& pos >= _start);
            iterator it = pos + 1;
            while (it != _finish)
            {
                *(it - 1) = *it;
                it++;
            }
            _finish;
            return pos;
        }
        T& operator[](size_t pos)
        {
            assert(pos < size());

            return _start[pos];
        }

        const T& operator[](size_t pos) const
        {
            assert(pos < size());

            return _start[pos];
        }
        void pop_back()
        {
            erase(--end());
        }
    private:
        iterator _start = nullptr;
        iterator _finish = nullptr;
        iterator _endofstorage = nullptr;


    };

}